#====  PACKAGE =================================================================
#         NAME:  Infection
#      PURPOSE:  A simple infection object. Nodes have a list of infections.
#   PARAMETERS:  name(String), states(Hash)
#      RETURNS:  True
#===============================================================================

#- Overview --------------------------------------------------------------------
# get_state_hash	get_timeline		remove_edge
# get_state		set_state		get_name
# set_name		is_infection
#-------------------------------------------------------------------------------

package insj_m_infection;
use strict;
use warnings;
use Carp;
@Node::ISA = qw();

#===  FUNCTION  ================================================================
#         NAME:  new
#      PURPOSE:  Constructor
#   PARAMETERS:  name(String)
#      RETURNS:  Infection Object
#        USAGE:  my $infection = Infection->new(name => "1");
#===============================================================================
sub new {

my ($class) = shift @_;
    my (%arg)   = @_;
    my %entry;
    if ( defined $arg{entry} ) {
        %entry = %{ $arg{entry} };
    }
    else {
        %entry = %arg;
    }
    	my %states;
	my @timeline;
	bless {
		_name     => $entry{name},
		_states   => \%states,
		_timeline => \@timeline
		}, $class;
}

#---------------------------------------------------------------------------
# getters
#---------------------------------------------------------------------------
sub get_name		{    $_[0]->{_name} }
sub get_state   	{ ${ $_[0]->{_states} }{$_[1]} }
sub get_state_hash	{ %{ $_[0]->{_states} } }
sub get_timeline	{ @{ $_[0]->{_timeline} } }

#---------------------------------------------------------------------------
# identifier
#---------------------------------------------------------------------------
sub is_infection { 1 }

#---------------------------------------------------------------------------
# setters
#---------------------------------------------------------------------------
sub set_name	{ $_[0]->{_name} = $_[1] }

#=====  SETTER  ================================================================
#         NAME:  set_state
#      PURPOSE:  sets the state
#   PARAMETERS:  state (STRING), value (INT), generation (INT)
#      RETURNS:  void
#        USAGE:  $node->set_state("infected", 0, 10);  
#===============================================================================
sub set_state	{ 
	${ $_[0]->{_states} }{ $_[1]} = $_[2] ; 
	my $string = "$_[1];$_[2];$_[3]";

	# Clean timeline, in case of same generation change!
	my @line = split(/\;/, $string);
	my %hash;
	my $i = 1;
	foreach (reverse  @{ $_[0]->{_timeline}}) {
		my @line2 = split(/;/);
		
		if ($line[0] eq $line2[0] and $line[2] eq $line2[2]) {
			splice( @{ $_[0]->{_timeline}}, -$i, 1 );
			}
		if ($line[2] > $line2[2]) {last;}
		$i++;
		
		}

	# Add element
	if ($string !~ '_t'){
		push( @{ $_[0]->{_timeline} }, $string);}	

	1;
	}

1;
